package madixin.t75;

class Solution {
    //三指针法
    public void sortColors(int[] nums) {
        int zero = 0;
        int one = 0;
        int two = nums.length - 1;
        while (one <= two) {
            if (nums[one] == 0) {
                swap(nums, zero, one);
                zero++;
                one++;
            } else if (nums[one] == 1) {
                one++;
            } else {
                swap(nums, one, two);
                two--;
            }
        }
    }

    void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        int[] nums = new int[]{2, 0, 1};
        s.sortColors(nums);
        for (int n : nums) {
            System.out.println(n);
        }
        //System.out.println(nums.toString());
    }
}